package com.compmov.libraries;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.compmov.datamodel.Persona;
import com.compmov.socialnetwork.Perfil;
import com.compmov.socialnetwork.PerfilAmigo;
import com.compmov.socialnetwork.R;
import com.compmov.webservice.PersonaWSClient;
import com.compmov.webservice.WSClientException;

public class LazyAdapter extends BaseAdapter {

	private Activity activity;
	private List<Persona> data;

	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public LazyAdapter(Activity a, List<Persona> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		public TextView textN;
		public TextView textC;
		public TextView textFN;
		public ImageView image;
		public ImageButton cancel;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.item_amigos, null);
			holder = new ViewHolder();
			holder.textN = (TextView) vi.findViewById(R.id.textN);
			;
			holder.textC = (TextView) vi.findViewById(R.id.textC);
			;
			holder.textFN = (TextView) vi.findViewById(R.id.textFN);
			;
			holder.image = (ImageView) vi.findViewById(R.id.image);
			holder.cancel = (ImageButton) vi.findViewById(R.id.cancel);
			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();

		Persona per = data.get(position);

		holder.textN.setText(per.getNombreCompleto());
		holder.textC.setText("Correo: " + per.getCorreo());
		holder.textFN.setText("Fec.Nac.: " + per.getFechaNacimiento());
		holder.image.setTag(per.getFotografia());
		imageLoader.DisplayImage(per.getFotografia(), activity, holder.image);

		holder.image.setId(per.getId());
		holder.image.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent nuevoIntent = new Intent(activity, PerfilAmigo.class);
				nuevoIntent.putExtra("idPer", view.getId());
				activity.startActivity(nuevoIntent);

			}
		});

		holder.cancel.setTag(position);
		holder.cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {

				int pos = (Integer) view.getTag();
				Persona per = data.get(pos);

				obtenAceptar(per);
			}
		});

		return vi;
	}

	private void obtenAceptar(final Persona persona) {
		final AlertDialog.Builder alert = new AlertDialog.Builder(activity);
		alert.setMessage("Desea eliminar a " + persona.getNombreCompleto()
				+ " de su lista de amigos?");

		alert.setPositiveButton("Aceptar",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						PersonaWSClient personaWS = new PersonaWSClient("persona");

						try {
							if (personaWS.quitaAmigo(
									com.compmov.libraries.Utils.getPersona().getId(), persona.getId())) {
								Intent intent = new Intent();
								intent.setClass(activity, Perfil.class);
								intent.putExtra("tipo", 2);
								activity.startActivity(intent);
								activity.finish();
							} else {
								Toast.makeText(activity,
										"Operacion no permitida intente de nuevo",
										Toast.LENGTH_SHORT).show();
							}

						} catch (WSClientException e) {
							// TODO Auto-generated catch block
							Toast.makeText(activity, e.getMessage(),
									Toast.LENGTH_SHORT).show();
						}

					}
				});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		alert.show();

	}
}